/**
 * (c) 2011 Patrick Long
 * All rights reserved.
 */

package net.novasharper.icarus.graphics;

import org.lwjgl.opengl.*;
import org.newdawn.slick.*;
import org.newdawn.slick.opengl.*;

public class Render2D {
	public static void drawSquare(double x, double y, int side, float[] color) {
		drawSquare(x, y, 0, side, color);
	}
	
	public static void drawSquare(double x, double y, int side, float[] color, boolean centered) {
		drawRect(new Location(x, y, 0), side, side, color, false);
	}
	
	public static void drawSquare(double x, double y, double rot, int side, float[] color) {
		drawSquare(new Location(x, y, rot), side, color);
	}
	
	public static void drawSquare(Location l, int side, float[] color) {
		drawRect(l, side, side, color, true);
	}
	
	public static void drawRect(Location l, int width, int height, float[] color, boolean centered) {
		double cX = l.x(), cY = l.y(), rot = l.rot();
		GL11.glLoadIdentity();
		GL11.glPushMatrix();
	    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
	    GL11.glTranslated(cX, cY, 0);
	    if(centered) GL11.glTranslated(-width / 2, -height / 2, 0);
	    GL11.glRotatef((float)rot, 0, 0, 1);
	    Color c = new Color(color[0], color[1], color[2]);
	    c.bind();
	    
	    // draw quad
	    GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2d((0),
        				(0));
		GL11.glVertex2d((width),
						(0));
		GL11.glVertex2d((width),
						(height));
		GL11.glVertex2d((0),
						(height));
	    GL11.glEnd();
	    GL11.glPopMatrix();
	}
	
	public static void drawTex(double x, double y, Texture tex) {
		double width = tex.getImageWidth(), height = tex.getImageHeight(), rot = 0;
	    drawTex(x, y, width, height, rot, width / 2, height / 2,  tex);
	}
	
	public static void drawTex(double x, double y, double rot, double cX, double cY, Texture tex) {
		double width = tex.getImageWidth(), height = tex.getImageHeight();
		drawTex(x, y, width, height, rot, cX, cY, tex);
	}
	
	public static void drawTex(double x, double y, double width, double height, Texture tex) {
		double rot = 0;
		drawTex(x, y, width, height, rot, width / 2, height / 2, tex);
	}
	
	public static void drawTex(double x, double y, double width, double height, double rot, double cX, double cY, Texture tex) {
		GL11.glLoadIdentity();
		GL11.glPushMatrix();
	    GL11.glTranslated(x, y, 0);
	    GL11.glRotatef((float)rot, 0, 0, 1);
		Color.white.bind();
		tex.bind();
	    
	    // draw quad
	    GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2d(0,0);
        GL11.glVertex2d((-width / 2 - (cX - width / 2)),
        				(-height / 2 - (cY - height / 2)));
		GL11.glTexCoord2d(1,0);
		GL11.glVertex2d((width / 2 - (cX - width / 2)),
						(-height / 2 - (cY - height / 2)));
		GL11.glTexCoord2d(1,1);
		GL11.glVertex2d((width / 2 - (cX - width / 2)),
						(height / 2 - (cY - height / 2)));
		GL11.glTexCoord2d(0,1);
		GL11.glVertex2d((-width / 2 - (cX - width / 2)),
						(height / 2 - (cY - height / 2)));
	    GL11.glEnd();
	    GL11.glPopMatrix();
	}
	
	public static void drawBg(Texture tex) {
		// set the color of the quad (R,G,B,A)
		GL11.glPushMatrix();
		GL11.glLoadIdentity();
		Color.white.bind();
	    //GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		tex.bind();
	    
	    // draw quad
	    GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2d(0,0);
        GL11.glVertex2d(0, 0);
		GL11.glTexCoord2d(1,0);
		GL11.glVertex2d(700 / tex.getWidth(), 0);
		GL11.glTexCoord2d(1,1);
		GL11.glVertex2d(700 / tex.getWidth(), 700 / tex.getHeight());
		GL11.glTexCoord2d(0,1);
		GL11.glVertex2d(0, 700 / tex.getHeight());
	    GL11.glEnd();
	    GL11.glPopMatrix();
	}
}
